//
//  LQL_filterViewController.m
//  Drops
//
//  Created by dllo on 16/5/23.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "LQL_filterViewController.h"
#define ScreenWidth [self.view bounds].size.width
#define ScreenHeight [self.view bounds].size.height

@interface LQL_filterViewController ()
<
    UIPickerViewDataSource,
    UIPickerViewDelegate
>
{
    NSMutableArray *_pickerData;//关系选择器数组
    UIPickerView *pickerView;//关系选择器
    UIImageView *imageView;//imageView
}
@property (nonatomic, retain) NSMutableDictionary *filterDic;
@end

@implementation LQL_filterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(clickRightButton)];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 50, 40);
    [leftBtn setImage:[UIImage imageNamed:@"back.png"] forState:0];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 50, 40);
    [rightBtn setImage:[UIImage imageNamed:@"done.png"] forState:0];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [rightBtn addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
    
    //单击手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicker)];
    tapGesture.numberOfTapsRequired = 1;//手势敲击的次数
    [self.view addGestureRecognizer:tapGesture];
    
    
    //源图片
    //imageView
    imageView = [[UIImageView alloc] initWithImage:self.inImage];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64);
//    imageView.center = self.view.center;
    [self.view addSubview:imageView];
    self.filterDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                      @"InImage", @"经典原图",
                      @"CIPhotoEffectMono", @"童年黑白",
                      @"CIPhotoEffectChrome", @"怀旧泛黄",
                      @"CIPhotoEffectFade", @"永恒褪色",
                      @"CIPhotoEffectInstant", @"青涩琉璃",
                      @"CIPhotoEffectNoir", @"典雅黑色",
                      @"CIPhotoEffectProcess", @"清新绿色",
                      @"CIPhotoEffectTonal", @"50度灰",
                      @"CIPhotoEffectTransfer", @"高清明亮",
                      @"CISRGBToneCurveToLinear", @"夜幕降临",
                      @"CIVignetteEffect", @"黑暗笼罩",  nil];
    _pickerData = [[NSMutableArray alloc] initWithObjects:
                   @"经典原图",
                   @"童年黑白",
                   @"怀旧泛黄",
                   @"永恒褪色",
                   @"青涩琉璃",
                   @"典雅黑色",
                   @"清新绿色",
                   @"50度灰",
                   @"高清明亮",
                   @"夜幕降临",
                   @"黑暗笼罩",
                   nil];
    [self addPickerView];
}
#pragma mark 滤镜处理事件
- (void)fliterEvent:(NSString *)filterName
{
    if ([filterName isEqualToString:@"InImage"]) {
        imageView.image = self.inImage;
        
    }else{
        //将UIImage转换成CIImage
        CIImage *ciImage = [[CIImage alloc] initWithImage:self.inImage];
        
        //创建滤镜
        CIFilter *filter = [CIFilter filterWithName:filterName keysAndValues:kCIInputImageKey, ciImage, nil];
        
        //已有的值不改变，其他的设为默认值
        [filter setDefaults];
        
        //获取绘制上下文
        CIContext *context = [CIContext contextWithOptions:nil];
        
        //渲染并输出CIImage
        CIImage *outputImage = [filter outputImage];
        
        //创建CGImage句柄
        CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
        
        //获取图片
        self.outImage = [UIImage imageWithCGImage:cgImage];
        
        //释放CGImage句柄
        CGImageRelease(cgImage);
        imageView.image = self.outImage;
    }
    
}

#pragma mark 开关选择器
- (void)showPicker
{
    static int i = 0;
    i++;
    
    if (i % 2 == 1) {
        //picker显示
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];//动画时间0.3s
        pickerView.frame = CGRectMake(0, ScreenHeight - CGRectGetHeight(pickerView.bounds), ScreenWidth, CGRectGetHeight(pickerView.bounds));
        [UIView commitAnimations];
    }else{
        //picker关闭
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];//动画时间0.3s
        pickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, CGRectGetHeight(pickerView.bounds));
        [UIView commitAnimations];
    }
}

#pragma mark - 选择器

- (void) addPickerView{
    //pickerView
    int pickerHeight = 200;//pickerView的高度
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, pickerHeight)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
    pickerView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.9];
    [self.view addSubview:pickerView];
    
}

//返回pickerview的组件数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}

//返回每个组件上的行数
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [_pickerData count];
}

//设置每行显示的内容
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [_pickerData objectAtIndex:row];
}

//当你选中pickerview的某行时会调用该函数。
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    [self fliterEvent:[self.filterDic objectForKey:[_pickerData objectAtIndex:row]]];
}
- (void)clickRightButton{
    [self.navigationController popViewControllerAnimated:YES];
    if (imageView.image == self.inImage) {
        self.filterBlock(self.inImage);
    } else {
        self.filterBlock(self.outImage);
    }
}

//- (void)dealloc{
//    Block_release(self.picBlock);
//    [super dealloc];
//}

- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
